module(..., package.seeall)
--- 模块功能：MQTT客户端处理框架
-- @author openLuat
-- @module mqtt.mqttTask
-- @license MIT
-- @copyright openLuat
-- @release 2018.03.28
require "misc"
require "mqtt"
require "mqttHostInMsg"
require "mqttHostOutMsg"
require "nvm"
require "config"
require "doorLocStatFeedback"
-- 初始化设备参数配置文件
nvm.init("config.lua", true)
-- 获取设备配置参数
local mqttHostHost, mqttPortHost, mqttKeepAliveHost, mqttLoginNameHost,
      mqttLoginPwmHost, mqttTransportHost, hostNo, alarmTime, openTime =
    nvm.get("MqttHostHost"), nvm.get("MqttPortHost"),
    nvm.get("MqttKeepAliveHost"), nvm.get("MqttLoginNameHost"),
    nvm.get("MqttLoginPwmHost"), nvm.get("MqttTransportHost"),
    nvm.get("HostNo"), nvm.get("AlarmTime"), nvm.get("OpenTime")

local ready = false
--- MQTT连接是否处于激活状态
-- @return 激活状态返回true，非激活状态返回false
-- @usage mqttTask.isReady()
function IsmqttHostReady() return ready end

-- 启动MQTT客户端任务
function StartHostMQTTClient(clientNo)
    sys.taskInit(function()
        local retryConnectCnt = 0
        while true do
            if not socket.isReady() then
                retryConnectCnt = 0
                -- 等待网络环境准备就绪，超时时间是5分钟
                sys.waitUntil("IP_READY_IND", 300000)
            end
            if socket.isReady() then
                -- 创建一个MQTT客户端
                local mqttClient = mqtt.client(clientNo, mqttKeepAliveHost,
                                               mqttLoginNameHost,
                                               mqttLoginPwmHost, nil, {
                    qos = 0,
                    retain = 0,
                    topic = "acs/" .. hostNo,
                    payload = "{\"action\":\"offline\",\"slaveNo\":\"" ..
                        clientNo .. "\"}"
                }, "3.1")
                local topic = "acs/" .. hostNo
                local sendMsg = "{\"action\":\"info\",\"slaveNo\":\"" ..
                                    clientNo .. "\",\"status\":\"" ..
                                    doorLocStatFeedback.GetGpio24Fnc() .. "\"}"
                log.info("sendMsg:",sendMsg,"发送报文:", sendMsg)
                -- 阻塞执行MQTT CONNECT动作，直至成功
                -- 如果使用ssl连接，打开mqttClient:connect("lbsmqtt.airm2m.com",1884,"tcp_ssl",{caCert="ca.crt"})，根据自己的需求配置
                -- mqttClient:connect("lbsmqtt.airm2m.com",1884,"tcp_ssl",{caCert="ca.crt"})
                if mqttClient:connect(mqttHostHost, mqttPortHost,
                                      mqttTransportHost) then
                    retryConnectCnt = 0
                    ready = true
                    mqttClient:publish(topic, sendMsg, 0)
                    -- 订阅主题
                    if mqttClient:subscribe({
                        ["acs/" .. clientNo] = 0,
                        ["acs/bind"] = 0
                    }) then
                        -- mqttOutMsg.init()
                        -- 循环处理接收和发送的数据
                        while true do
                            if not mqttHostInMsg.HostInsertProc(mqttClient) then
                                log.error("mqttTask.mqttHostInMsg.proc error")
                                break
                            end
                            if not mqttHostOutMsg.HostOutProc(mqttClient) then
                                log.error("mqttTask.mqttHostOutMsg.proc error")
                                break
                            end
                        end
                    end
                    ready = false
                else
                    retryConnectCnt = retryConnectCnt + 1
                end
                -- 断开MQTT连接
                mqttClient:disconnect()
                if retryConnectCnt >= 5 then
                    link.shut()
                    retryConnectCnt = 0
                end
                sys.wait(5000)
            else
                -- 进入飞行模式，20秒之后，退出飞行模式
                net.switchFly(true)
                sys.wait(20000)
                net.switchFly(false)
            end
        end
    end)
end

